// featurePackage/pages/mySave/mySave.js
import {
  collectList,
  cancelCollect
} from "../../../service/jb_common_service"
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    menuTop: app.globalData.menuButton.top,
    menuHeight: app.globalData.menuButton.height,
    topTabIndex: 0,
    topTabType: 'product',
    dataList: [
      // {
      //   "productId": 9,
      //   "image": "https://images.jiabao19.com/images/product/2023-12-30/1703949611412/555555.webp",
      //   "storeName": "测试商品",
      //   "price": "0.10",
      //   "otPrice": "166.00",
      //   "sales": 0,
      //   "ficti": null,
      //   "unitName": null,
      //   "activity": "0,1,2,3",
      //   "activityH5": null,
      //   "cartNum": null,
      //   "stock": null,
      //   "flatPattern": null,
      //   "id": 1,
      //   "uid": null,
      //   "littleShopSales": 0
      // },
      // {
      //   "productId": 8,
      //   "image": "https://images.jiabao19.com/images/public/maintain/2021/12/25/7a6475bba07c47bb98e5db0e0d21c8ebq5sw2kijlc.jpg",
      //   "storeName": "兰蔻（LANCOME）口红196 全新菁纯丝绒雾面唇膏 化妆品礼盒",
      //   "price": "787.00",
      //   "otPrice": "197.00",
      //   "sales": 0,
      //   "ficti": null,
      //   "unitName": null,
      //   "activity": "0,1,2,3",
      //   "activityH5": null,
      //   "cartNum": null,
      //   "stock": null,
      //   "flatPattern": null,
      //   "id": 3,
      //   "uid": null,
      //   "littleShopSales": 0
      // },
      // {
      //   "productId": 6,
      //   "image": "https://images.jiabao19.com/images/public/maintain/2021/12/25/fef1aee22a04466d93693f5791d44bfd2rsm469mcr.jpg",
      //   "storeName": "第一江南 西湖龙井茶叶礼盒 明前特级龙井绿茶【至臻献礼200g】礼盒装",
      //   "price": "1588.00",
      //   "otPrice": "1688.00",
      //   "sales": 0,
      //   "ficti": null,
      //   "unitName": null,
      //   "activity": "0,1,2,3",
      //   "activityH5": null,
      //   "cartNum": null,
      //   "stock": null,
      //   "flatPattern": null,
      //   "id": 4,
      //   "uid": null,
      //   "littleShopSales": 0
      // }
    ],
    requestData: {
      category: 'product',
      page: 1,
      limit: 10,
    },

    refreshFlag: false,
    isBottomShow: false,
    isShowLoading: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.queryCollectList('refresh');
  },

  topChangeAction(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      topTabIndex: index,
    })
    if (index == 0) {
      this.setData({
        'requestData.category': 'product'
      })
    } else {
      this.setData({
        'requestData.category': 'article'
      })
    }
    this.setData({
      'refreshFlag.page': 1,
      dataList: [],
    })
    this.queryCollectList('refresh');
  },
  queryCollectList(type) {
    wx.showLoading();
    collectList(this.data.requestData).then((res) => {
      wx.hideLoading()
      console.log("JJLog:res:" + JSON.stringify(res));
      if (res.code == 200) {
        let arr = (type == 'refresh' ? [] : this.data.dataList || []);
        arr = arr.concat(res.data.list || [])
        this.setData({
          dataList: arr,
          refreshFlag: false,
          isShowLoading: false,
          isBottomShow: res.data.totalPage == this.data.requestData.page ? true : false,
        })
        console.log("JJLog:看看isBottomShow:" + this.data.isBottomShow)

      }
    }).catch((err) => {
      wx.hideLoading()
      wx.showToast({
        title: err || '加载失败',
        icon: "none"
      })
      if (this.data.requestData.page != 1) {
        this.setData({
          'requestData.page': this.data.requestData.page - 1
        })
      }
      this.setData({
        refreshFlag: false,
      })
      console.log("JJLog:collectList:" + JSON.stringify(err));
    });
  },

  //收藏操作：
  collectAction(e) {
    let knoData = e.detail.data;
    let index = e.detail.index;
    this.handlerArticleCancelCollect(knoData, index);


  },
  handlerArticleCancelCollect(knoData, index) {
    wx.showLoading();
    cancelCollect({
      category: "article",
      productId: knoData.productId
    }).then(res => {
      wx.hideLoading()
      if (res.code == 200) {
        let arr = this.data.dataList;
        arr.splice(index, 1)
        this.setData({
          dataList: arr,
        })
      }
    }).catch(err => {
      wx.hideLoading()
      wx.showToast({
        title: err || '操作失败',
        icon: "none"
      })
    })
  },
  //操作取消收藏
  handleCancelSave(e) {
    // console.log("JJLog:e:" + JSON.stringify(e));
    let index = e.detail.index;
    let id = this.data.dataList[index].productId;//商品取消收藏是用这个productId哦。
    wx.showLoading();
    cancelCollect(
      {
        category: "product",
        productId: id
      }
    ).then((res) => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: '取消收藏成功',
          icon: "success"
        })
        this.queryCollectList("refresh");
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || '操作失败',
        icon: "none"
      })
      console.log("JJLog:cancelCollect:" + JSON.stringify(err));

    })
  },
  // 下拉刷新
  handleRefresh() {
    // TODO
    this.setData({
      'requestData.page': 1,
      isBottomShow: false,
      refreshFlag: true,
    })
    this.queryCollectList('refresh');
  },

  // 加载更多
  handleLoadmore() {
    console.log('加载更多');
    if (this.data.isBottomShow) {
      return;
    }
    this.setData({
      isShowLoading: true,
      'requestData.page': this.data.requestData.page + 1
    })
    this.queryCollectList('')
  },
})